package com.demo.dao.impl;

import com.demo.dao.StageDao;
import com.demo.dto.StageDto;
import com.demo.pojo.Project;
import com.demo.pojo.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName StageDaoImpl
 * @Description TODO
 * @Author Windy
 * @Date 2022/9/2 16:41
 * @Version 1.0
 **/
@Repository
public class StageDaoImpl implements StageDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public StageDto findStageById(String id) {
        List<Stage> query = jdbcTemplate.query("select * from stage where sid=?;",
                new BeanPropertyRowMapper<Stage>(Stage.class),id);
       if (query.size() <= 0) {
           return null;
       }
            StageDto dto = new StageDto();

            List<Project> project = jdbcTemplate.query("select * from project where pid=?",
                    new BeanPropertyRowMapper<Project>(Project.class), query.get(0).getPid());
            dto.setStage(query.get(0));
            if (project.size() > 0) {
                dto.setProject(project.get(0));
            }
        return dto;
    }

    @Override
    public StageDto findStageByName(String name) {
        List<Stage> query = jdbcTemplate.query("select * from stage where sname=?;",
                new BeanPropertyRowMapper<Stage>(Stage.class),name);
        if (query.size() <= 0) {
            return null;
        }
        StageDto dto = new StageDto();

        List<Project> project = jdbcTemplate.query("select * from project where pid=?",
                new BeanPropertyRowMapper<Project>(Project.class), query.get(0).getPid());
        dto.setStage(query.get(0));
        if (project.size() > 0) {
            dto.setProject(project.get(0));
        }
        return dto;
    }

    @Override
    public int deletedStage(Stage stage) {
        int update = jdbcTemplate.update("update stage set status=0 where sid=? ", stage.getSid());
        return update;
    }

    @Override
    public int updateStageById(Stage stage) {
        Object[] obj={stage.getSname(),stage.getStarttime(),stage.getEndtime(),stage.getPid(),
                stage.getUpdateTime(),stage.getStatus(),stage.getSid()};
        int update = jdbcTemplate.update("update stage set sname=?,starttime=?,endtime=?,pid=?,updatetime=?,status=? where " +
                "sid=?;", obj);
        return update;
    }

    @Override
    public int insertStage(Stage stage) {
        Object[] obj={stage.getSid(),stage.getSname(),stage.getStarttime(),stage.getEndtime(),stage.getPid(),
                stage.getUpdateTime(),stage.getStatus()};
        int update = jdbcTemplate.update("insert into stage values(?,?,?,?,?,?,?)", obj);
        return update;
    }

    @Override
    public List<StageDto> findAll() {
        List<Stage> query = jdbcTemplate.query("select * from stage;",
                new BeanPropertyRowMapper<Stage>(Stage.class));
        List<StageDto> lists=new ArrayList<>();
        for (Stage stage : query) {
            StageDto dto = new StageDto();

            List<Project> project = jdbcTemplate.query("select * from project where pid=?",
                    new BeanPropertyRowMapper<Project>(Project.class), stage.getPid());

//            System.out.println(project);
            dto.setStage(stage);
            if (project.size() > 0) {
                dto.setProject(project.get(0));
            }

            lists.add(dto);
        }
        return lists;
    }
}
